﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2010-8-29
 * Time: 11:51
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Reflection;

namespace wincssdemo
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			
			List<Control> element;
			string src=textBox1.Text;
			Regex elm= new Regex(@"(?<elm>[\w]+?)\{\s*[\w\W]+?\}");
			Match m=elm.Match(src);

			if(m.Success)treeView1.Nodes.Clear();
				while(m.Success){
					treeView1.Nodes.Add(m.Groups["elm"].Value);
					element=getelement(Controls,m.Groups["elm"].Value);
					Regex child= new Regex(@"\s*(?<prop>[-\w]+?)\:(?<val>[\s\.#\w]+?)\;");
				Match cm= child.Match(m.Value);

				while(cm.Success)
					{
						object  value=genparm(cm.Groups["val"].ToString(),cm.Groups["prop"].ToString());
						foreach(Control i in element)
						{
							setpropvalue(i,cm.Groups["prop"].ToString(),value);
						
						}
						treeView1.Nodes.Add(cm.Groups["prop"]+":"+cm.Groups["val"]);
						cm=cm.NextMatch();
							
							
					}

					m=m.NextMatch();
				
				}
		
		}
		
		public List<Control> getelement(System.Windows.Forms.Control.ControlCollection src,string key)
		{
			List<Control> cs=new List<Control>();
		
			foreach(Control c in src)
			{
				System.Diagnostics.Debug.Print(c.GetType().ToString());
				if(c.GetType().ToString().ToLower()=="System.Windows.Forms.".ToLower()+key.ToLower())
				{
					//System.Diagnostics.Debug.Print(c.GetType().ToString());
					cs.Add(c);
				}
			}
			if(key=="Form")
				cs.Add(this);
			
			return cs;
		}
		public void setpropvalue(object o,string name,object val)
		{
			try{
			Type t= o.GetType();
			
			PropertyInfo pi= t.GetProperty(name);
			System.Diagnostics.Debug.Print(pi.PropertyType.ToString());
			
			Type ty=pi.PropertyType;
			
			object v= genval(ty,val.ToString());
			
			
			
			pi.SetValue(o,val,null);
			}
			catch(Exception ex)
			{}

			
			 
		}
		public object genval(Type t,string val)
		{
			try{
			object  result;
			MethodInfo tmp=t.GetMethod("Parse",new Type[]{typeof(String)});
			if(tmp != null)
			{
				result=t.InvokeMember("Parse",BindingFlags.Public|BindingFlags.Instance
				                      |BindingFlags.Static|BindingFlags.InvokeMethod,
            					 null,null,
            					 new string[]{val} );
				return result;
				
				
			}
			}
			catch(Exception ex)
			{
			
			}
			return null;
		
		}
		public object genparm(string src,string type)
		{
			try{
			switch(type){
			case "BackColor":
					{
				return ColorTranslator.FromHtml(src);
					}
			case "Width":
			case "Height":
					{
				return Int32.Parse(src);
				
					}
			case "BackgroundImage":
					{
				return Bitmap.FromFile(src);
				
					}
			case "Enabled":
			case "Visible":
					{
				return  src.ToLower()=="true"?true:false;
					}
					case "Size":
						{
							string[] val=src.Trim().Split(' ');
							return new Size(int.Parse(val[0]),int.Parse(val[1]));
						
						}
					case "Location":
						{
							string[]val= src.Trim().Split(' ');
							return new Point(int.Parse(val[0]),int.Parse(val[1]));
						}
			 
			
				default:
					return null;
				
			}
			}
			catch(Exception ex){
				return null;
				
			}
			
		}
		
	
		
		void Button2Click(object sender, EventArgs e)
		{
			setpropvalue(button2,"BackColor",Color.Green);
			setpropvalue(button2,"Width","100");
			setpropvalue(button2,"BackColor",Color.Green);
			
		//	setpropvalue(button2,"Location",new Point(10,20));
			//setpropvalue(button2,"FlatStyle","Popup");
		}
	}
}
